# -*- mode: python; coding: utf-8; -*-
##--------------------------------------------------------------------------##
##
## Copyright (C) 1998-2003 Markus Oberhumer <markus@oberhumer.com>
## Copyright (C) 2003 Mt. Hood Playing Card Co.
## Copyright (C) 2005-2010 Skomoroh <skomoroh@users.sourceforge.net>
## Copyright (C) 2010-2013 Jeremy Austin-Bardo <tjaustinbardo@gmail.com>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##--------------------------------------------------------------------------##
__docformat__ = "restructuredtext en"

from .. import formlib as layout

LEFT, RIGHT = layout.LEFT, layout.RIGHT


class CalculateLayout(layout.Layout):
    """
    - top: foundations over many cols. Default 1 suit set.
    - below: rows. Vert offset when playcards
    - left: waste, talon. Waste with horz offset when wastecards

    """

    def defaultAll(self):
        """
        defaultAll() -> None

        Set default for texts, groups, and regions.
        """
        game = self.game
        foundations = self.s['foundations']
        if game.s['foundations'] and foundations:
            for gr, lr in zip(game.s['foundations'], foundations):
                if lr.text:
                    self.set_misc_text(gr, **lr.text)
        super(CalculateLayout, self).defaultAll()

    def create(self, rows, ncols=None, playcards=12, bases=None,
               reserves=0, texts=False, **kw):
        l, s = self, self.s
        ncols = ncols or rows
        bases = bases if type(bases) in (list, tuple) else l.SUITS
        max_rows = max(l.SUIT, rows)
        dy = (len(bases) / l.NSUIT + int(len(bases) % l.NSUIT > 0)) * l.HYS
        l._set_horz_talon(y=dy, texts=texts, align=LEFT, anchor='S',
                          wastecards=0)
        s['talon'].set_rounds(anchor='sss' if l._has_waste else 'e')
        dx = l.size.width
        center = lambda p: dx + (max_rows - p) * l.HXS
        l._set_horz_foundations(x=center(rows), y=l.YM, size=l.NSUIT,
                                bases=bases)
        for foundation in s['foundations']:
            foundation.set_text(anchor='s')
        l._set_rows(x=center(rows / ncols), y=l.size.height + l.TEXT_HEIGHT,
                    rows=rows, ncols=ncols, ycards=playcards)